package com.kuwou.workobject.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.kuwou.workobject.Utils.QueryUtil;
import com.kuwou.workobject.commonality.Log;
import com.kuwou.workobject.pojo.dto.MusicDto;
import com.kuwou.workobject.pojo.entity.Channel;
import com.kuwou.workobject.pojo.entity.Music;
import com.kuwou.workobject.pojo.entity.RankList;
import com.kuwou.workobject.mapper.RankListMapper;
import com.kuwou.workobject.pojo.vo.MusicVo;
import com.kuwou.workobject.pojo.vo.RankListVo;
import com.kuwou.workobject.service.RankListService;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.*;

@RequiredArgsConstructor
@Service
public class RankListServiceImpl extends ServiceImpl<RankListMapper, RankList> implements RankListService {



    private final @Lazy RankListMapper rankListMapper;




    private Logger logger = null;


    {
        //获取日志对象
        logger = Log.getLogger(MusicServiceImpl.class);
    }

    @Override
    public void truncateTable() {
        rankListMapper.truncateTable();
    }

    @Override
    public List<RankListVo> getRankListTop10() {
        return BeanUtil.copyToList(list(), RankListVo.class);
    }

    @Override
    public List<MusicVo> getMusicByRankListId(Integer id) {
        LambdaQueryWrapper<Music> queryWrapper = new LambdaQueryWrapper<>(Music.class)
                .eq(Music::getRankListId, id)
                .lt(Music::getRank, 11);
        List<Music> music = Db.list(queryWrapper);
        return BeanUtil.copyToList(music, MusicVo.class);
    }


    @Override
    public List<RankListVo> getRankList() {
        return BeanUtil.copyToList(Db.list(RankList.class), RankListVo.class);
    }

    //获取榜单数据
    @Override
    public List<RankList> getRankData() {
        logger.info("前端获取榜单数据");
        return  rankListMapper.getRankData();
    }

    @Override
    public List<MusicVo> getRankHotMusic() {
        logger.info("前端获取榜单热度歌曲数据");
        Map<Integer,String> channelMap = new HashMap<>();
        List<Channel> channelList = QueryUtil.getAllChannel();
        channelList.forEach(channel -> {
            channelMap.put(channel.getId(),channel.getName());
        });

        Map<Integer,String> rankMap = new HashMap<>();
        List<RankList> rankLists = list();
        rankLists.forEach(rankList -> {
            rankMap.put(rankList.getId(),rankList.getName());
        });

        List<Music> musicList = QueryUtil.getAllMusic();
        Map<String, MusicDto> hotMusic = new TreeMap<>();

        musicList.forEach(music -> {
            MusicDto musicDto;
            String musicName = music.getName();
            if (hotMusic.containsKey(musicName)) {
                musicDto = BeanUtil.copyProperties(music, MusicDto.class);
                musicDto.setCount(hotMusic.get(musicName).getCount() + 1);
                hotMusic.put(musicName,musicDto);
            } else {
                musicDto = BeanUtil.copyProperties(music, MusicDto.class);
                musicDto.setCount(1);
                hotMusic.put(musicName,musicDto);
            }
            musicDto.setChannelName(channelMap.get(music.getChannelId()));
            musicDto.setRankListName(rankMap.get(music.getRankListId()));
        });

//         排序
        List<MusicVo> musicVos =
                hotMusic.values()
                        .stream()
                        .sorted(Comparator.comparingInt(MusicDto::getCount).reversed())
                        .limit(10)
                        .map(musicDto -> BeanUtil.copyProperties(musicDto, MusicVo.class))
                        .toList();

//        List<MusicVo> musicVos = new ArrayList<>();
//        for (int i = 0; i < 10; i++) {
//            musicVos.add(BeanUtil.copyProperties(musicDtos.get(i), MusicVo.class));
//        }

        return musicVos;
    }
}
